package com.fqgj.xjd.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fqgj.common.api.Response;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.StringUtils;
import com.fqgj.exception.common.ApiErrorException;
import com.fqgj.xjd.user.client.UserLogService;
import com.fqgj.xjd.user.client.response.UserActionLogResponse;
import com.fqgj.xjd.user.dao.UserActionLogDao;
import com.fqgj.xjd.user.entity.UserActionLogEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2019-05-10
 * Time: 17:22
 */
@Service("userLogService")
public class UserLogServiceImpl implements UserLogService {

    public static final Logger LOGGER = LoggerFactory.getLogger(UserLogServiceImpl.class);

    @Autowired
    private UserActionLogDao actionLogDao;

    @Override
    public Response log(String action, String detail) {
        LOGGER.info("== UserLogService ==,action:{,detail:{}", action, detail);
        if (StringUtils.isEmpty(action)) {
            throw new ApiErrorException("action is empty");
        }

        if ("marketLogin".equals(action)) {
            marketLog(detail);
        }
        return Response.ok();
    }


    @Override
    public Response<List<UserActionLogResponse>> queryByTypeAndValues(Integer type, List<String> values) {
        if (CollectionUtils.isEmpty(values)) {
            return Response.ok();
        }

        List<UserActionLogEntity> userActionLogEntities = actionLogDao.selectListByTypeAndValues(type, values);
        if (CollectionUtils.isEmpty(userActionLogEntities)) {
            return Response.ok();
        }

        List<UserActionLogResponse> responses = new ArrayList<>();
        for (UserActionLogEntity log : userActionLogEntities) {
            UserActionLogResponse res = new UserActionLogResponse()
                    .setActionType(type)
                    .setActionValue(log.getActionValue())
                    .setChannel(log.getChannel());
            responses.add(res);
        }

        return Response.ok().putData(responses);
    }

    private void marketLog(String detail) {
        JSONObject jsonObject = JSON.parseObject(detail);
        String mobile = jsonObject.getString("mobile");
        String channel = jsonObject.getString("channel");

        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(channel)) {
            LOGGER.error("== marketLog 为空 ==");
            return;
        }

        // TODO: 2019-05-10 tag类型枚举
        UserActionLogEntity entity = new UserActionLogEntity()
                .setActionTagType(1)
                .setActionValue(mobile)
                .setChannel(channel)
                .setCreatedDt(new Date());

        actionLogDao.insert(entity);
    }


}
